package com.yjc.hire.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.github.pagehelper.PageHelper;
import com.yjc.bo.NewArticleBO;
import com.yjc.hire.enums.ArticleStatus;
import com.yjc.hire.grace.result.GraceJSONResult;
import com.yjc.hire.grace.result.PageGridResult;
import com.yjc.hire.server.mapper.ArticleMapper;
import com.yjc.hire.server.service.ArticleService;
import com.yjc.hire.utils.LocalDateUtils;
import com.yjc.pojo.Admin;
import com.yjc.pojo.Article;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/9/10
 * </p>
 */

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Transactional
    @Override
    public void createArticle(NewArticleBO article) {
        LocalDateTime time = article.getPublishTime();
        Article newArticle = new Article();
        if (time == null){
            newArticle.setStatus(ArticleStatus.OPEN.type);
        }
        else {
            newArticle.setStatus(ArticleStatus.CLOSE.type);
            newArticle.setPublishTime(time);
        }
        newArticle.setCreateTime(LocalDateTime.now());
        newArticle.setUpdateTime(LocalDateTime.now());
        articleMapper.insert(newArticle);
        String articleId = newArticle.getId();
        //扔给mq延迟发送
        if (time != null){
            LocalDateTime now = LocalDateTime.now();
//            long unitBetween = LocalDateUtils.getChronoUnitBetween(now, time, ChronoUnit.MILLIS, true);
            long delayTime = 10 * 1000;

        }
    }

    @Override
    public PageGridResult<List<Article>> list(Integer page, Integer limit,ArticleStatus status) {
        PageHelper.startPage(page,limit);

        List<Article> list = articleMapper.selectList(
                new LambdaQueryWrapper<Article>()
                        .eq(status != null, Article::getStatus, status.type)
                        .orderByDesc(Article::getPublishTime)
        );
        return PageGridResult.of(list,page);
    }

    //查询文章详情
    @Override
    public Article queryArticleDetail(String articleId,ArticleStatus status) {
        Article article = articleMapper.selectOne(
                new LambdaQueryWrapper<Article>()
                        .eq(status != null, Article::getStatus, status.type)
                        .eq(articleId != null, Article::getId, articleId)

        );
        return article;
    }

    @Override
    public void updateArticle(NewArticleBO article) {
        Article article1 = new Article();
        BeanUtils.copyProperties(article,article1);
        article1.setUpdateTime(LocalDateTime.now());
        article1.setId(article.getArticleId());
        articleMapper.updateById(article1);
    }
}
